package ModelLayer;
import java.util.ArrayList;
/**
 * A singleton class to hold a list of products
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class ProductContainer
{
    //Collections
    private ArrayList<Product> products;
    //Fields
    private static ProductContainer instance = null;
    
    /**
     * Constructor for objects of class ProductContainer
     * On creation adds 3 dummy products to the list
     * Singleton
     */
    private ProductContainer()
    {
        products = new ArrayList<Product>();
        Product product1 = new Product("Rockwool", "Isolering", "10101", 1, 100, 250, 49);
        Product product2 = new Product("Granplanke 3 tommer", "Byggemateriale", "10102", 2, 100, 125, 24);
        Product product3 = new Product("Skumrør", "Isolering", "10103", 1, 50, 79, 0);
        products.add(product1);
        products.add(product2);
        products.add(product3);
        
    }
    
    /**
     * Returns an isntance of OrderContainer. If one allready exists 
     * returns that one. Otherwise creates a new one
     */
    public static ProductContainer getInstance()
    {
        if (instance == null)
        {
            instance = new ProductContainer();
        }
        return instance;
    }
    
    /**
     * Adds a product to the list
     */
    public void addProduct(Product product)
    {
        products.add(product);
    }
    
    /**
     * Returns a product from tht list found by productName
     */
    public Product findProductByKeyWord(String keyWord)
    {
        int i = 0;
        boolean complete = false;
        Product searchProduct = null;
        while (i < products.size() && !complete)
        {
            if (products.get(i).getProductName().equals(keyWord))
            {
                searchProduct = products.get(i);
                complete = true;
            }
        }
        return searchProduct;
    }
    
    
    /**
     * Returns a product from the list found by productID
     */
    public Product findProductByID(String searchID)
    {
        int i = 0;
        boolean complete = false;
        Product searchProduct = null;
        while (i < products.size() && !complete)
        {
            if (products.get(i).getProductID() == searchID)
            {
                searchProduct = products.get(i);
                complete = true;
            }
        }
        return searchProduct;
    }
    
    /**
     * Returns a product from the list found by EAN
     */
    public Product findProductByEAN(String searchEAN)
    {
        int i = 0;
        boolean found = false;
        Product searchProduct = null;
        while(!found && i < products.size())
        {
            if(products.get(i).getEAN().equals(searchEAN))
            {
                searchProduct = products.get(i);
                found = true;
            }
            else
            {
                i++;
            }
        }
        
        if(searchProduct != null)
        {
            return searchProduct;
        }
        else
        {
            return null;
        }
    }
    
    /**
     * Removes a product from the list determined by productID
     */
    public boolean deleteProduct(String searchID)
    {
        Product productToDelete = findProductByID(searchID);
        if (productToDelete != null)
        {
            products.remove(findProductByID(searchID).getProductID());
            return true;
        }
        else
        {
            return false;
        }
    }
        
}
